<?php

namespace app\common\model;

/**
 * 微信小程序diy页面模型
 * Class WxappPage
 * @package app\common\model
 */
class WxappPage extends BaseModel
{
    protected $name = 'wxapp_page';

    /**
     * 格式化页面数据
     * @param $json
     * @return array
     */
    public function getPageDataAttr($json)
    {
        $array = json_decode($json, true);
        return compact('array', 'json');
    }

    /**
     * 自动转换data为json格式
     * @param $value
     * @return string
     */
    public function setPageDataAttr($value)
    {
        return json_encode($value ?: ['items' => []]);
    }

    /**
     * diy页面详情
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail()
    {
        return self::get([]);
    }

    /**
     * 新增小程序首页diy默认设置
     * @param $wxapp_id
     * @return false|int
     */
    public function insertDefault($wxapp_id)
    {
        return $this->save([
            'page_type' => 10,
            'page_data' => [
                'items' => [
                    's10001' => [
                        'id' => 's10001',
                        'type' => 'search',
                        'params' => ['placeholder' => '搜索商品'],
                        'style' => [
                            'textAlign' => 'center',
                            'searchStyle' => 'radius',
                        ],
                    ],
                    's10002' => [
                        'id' => 's10002',
                        'type' => 'banner',
                        'style' => [
                            'btnColor' => '#ffffff',
                            'btnShape' => 'round',
                        ],
                        'data' => [
                            'sd10001' => [
                                'imgUrl' => '',
                                'imgName' => 'banner-1.jpg',
                                'linkUrl' => '',
                            ],
                            'sd10002' => [
                                'imgUrl' => '',
                                'imgName' => 'banner-2.jpg',
                                'linkUrl' => '',
                            ],
                        ],
                    ]
                ],
            ],
            'wxapp_id' => $wxapp_id
        ]);
    }

}
